package com.spark.modules.generation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.core.Result;
import com.spark.common.core.entity.generation.GenModelFields;
import com.spark.common.query.QueryGenerator;
import com.spark.modules.generation.entity.vo.GenModelVo;
import com.spark.modules.generation.service.IGenModelFieldsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 模型字段Controller
 *
 * @author LYCHEE
 * @date 2025/02/24 15:25
 */
@RestController
@RequestMapping(value = "/gen/modelFields")
public class GenModelFieldsController {

    @Resource
    private IGenModelFieldsService genModelFieldsService;

    /**
     * 分页列表查询
     *
     * @param genModelFields 模型字段对象
     * @param pageNo         分页
     * @param pageSize       分页
     * @return 模型字段列表
     */
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(GenModelFields genModelFields,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<GenModelFields> queryWrapper = QueryGenerator.initQueryWrapper(genModelFields);
        Page<GenModelFields> page = new Page<GenModelFields>(pageNo, pageSize);
        IPage<GenModelFields> pageList = genModelFieldsService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param genModelFields 模型字段对象
     * @return 模型字段列表
     */
    @GetMapping(value = "/queryList")
    public Result<?> queryList(GenModelFields genModelFields) {
        QueryWrapper<GenModelFields> queryWrapper = QueryGenerator.initQueryWrapper(genModelFields);
        List<GenModelFields> list = genModelFieldsService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 模型字段对象
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        GenModelFields genModelFields = genModelFieldsService.getById(id);
        if (Objects.isNull(genModelFields)) {
            return Result.fail("数据获取失败");
        }
        return Result.ok(genModelFields);
    }

    /**
     * 新增
     *
     * @param genModelFields 模型字段对象
     * @return 成功或失败
     */
    @PostMapping(value = "/add")
    public Result<?> add(@Validated @RequestBody GenModelFields genModelFields) {
        boolean save = genModelFieldsService.save(genModelFields);
        return Result.flag(save);
    }

    /**
     * 编辑
     *
     * @param genModelFields 模型字段对象
     * @return 成功或失败
     */
    @PostMapping(value = "/edit")
    public Result<?> edit(@Validated @RequestBody GenModelFields genModelFields) {
        boolean update = genModelFieldsService.updateById(genModelFields);
        return Result.flag(update);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) {
        boolean update = genModelFieldsService.removeById(id);
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = genModelFieldsService.removeBatchByIds(Arrays.asList(ids));
        return Result.flag(update);
    }

    /**
     * 通过模型编码查询字段
     *
     * @param modelCode 模型code
     * @return 字段信息
     */
    @GetMapping(value = "/queryModelFields")
    public Result<?> queryModelFields(@RequestParam String modelCode) {
        return genModelFieldsService.queryModelFields(modelCode);
    }

    /**
     * 新增或编辑字段信息
     *
     * @param modelVo 模型信息
     * @return 是否成功
     */
    @PostMapping(value = "/saveOrEdit")
    public Result<?> saveOrEdit(@RequestBody GenModelVo modelVo) {
        return genModelFieldsService.saveOrEdit(modelVo);
    }

}
